package com.wandian.news.mvvm.lifetool.constellation;

import android.content.Context;
import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.dialog.BottomRecyclePopupWindow;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.DialogUtil;
import com.mylib.libcore.utils.TimeUtils;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.widget.datetimepicker.DateTimePicker;
import com.wandian.news.R;
import com.wandian.news.adapter.LifeConstellationRecycleAdapter;
import com.wandian.news.databinding.NewsActivityLifeBrithDayBinding;
import com.wandian.news.databinding.NewsActivityLifeConstellationBinding;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 星座activity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_NEWS_CONSTELLATION)
public class LifeConstellationActivity extends BaseMVVMActivity<LifeConstellationViewModel> implements BottomRecyclePopupWindow.ItemClink   {
    private NewsActivityLifeConstellationBinding dataBinding;
    private LifeConstellationRecycleAdapter adapter;
    private Date chooseData = null;
  //  十二星座英文名：aries 白羊座, taurus 金牛座, gemini 双子座, cancer 钜蟹座, leo 狮子座, virgo 处女座, libra 天枰座, scorpio 天蝎座, sagittarius 射手座, capricorn ,摩羯座, aquarius 水瓶座, pisces 双鱼座。
    private String constellationName = "白羊座";//白羊座
    private List<String> constellationList = new ArrayList<>();
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.news_activity_life_constellation);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        String constellation = AppContext.getInstance().getAppPreferencesHelper().getKeyValue("constellation");
        if(!UIUtils.isEmpty(constellation))constellationName = constellation;
        chooseData = new Date();
        initViewData();
        initClink();
        initRecycle();
    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("星座运势");
        constellationList.clear();
        constellationList.add("白羊座");
        constellationList.add("金牛座");
        constellationList.add("双子座");
        constellationList.add("钜蟹座");
        constellationList.add("狮子座");
        constellationList.add("处女座");
        constellationList.add("天枰座");
        constellationList.add("天蝎座");
        constellationList.add("射手座");
        constellationList.add("摩羯座");
        constellationList.add("水瓶座");
        constellationList.add("双鱼座");
        dataBinding.tvChooseXz.setText(constellationName);
        dataBinding.tvChooseData.setText(TimeUtils.getDateTime(chooseData)+"");
        //获取今日数据
        getDataConstellation(chooseData);
        mViewModel.dataBean.observe(this,dataBean->{
            if(!UIUtils.isEmpty(dataBean) && dataBean.size()>0){
                //dataBinding.tvZhzs.setText(dataBean.getContent().get);
                adapter.setNewData(dataBean);
            }
        });
    }

    private void initRecycle(){
       adapter = new LifeConstellationRecycleAdapter(R.layout.news_item_life_constellation_style);
       RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
       dataBinding.recycler.setLayoutManager(layoutManager);
       dataBinding.recycler.setAdapter(adapter);
    }


    private void initClink(){
        dataBinding.bindTransparentToolBar.toolBarIvBack.setOnClickListener(v -> finish());
        dataBinding.tvChooseData.setOnClickListener(v -> showYearDateDialog(this));
        dataBinding.tvChooseXz.setOnClickListener(v ->  DialogUtil.showCommonBottomRecycleDialog(dataBinding.llTop,this::itemTextClink,"xz",constellationList));
    }

    //日期选择器
    private  void showYearDateDialog(Context context) {
        DateTimePicker dateTimePicker = new DateTimePicker.Builder(context)
                .setCancelTextColor(context.getResources().getColor(com.mylib.libcore.R.color.text_black))
                .setOkTextColor(context.getResources().getColor(com.mylib.libcore.R.color.button_blue_normal))
                .setTitleTextColor(context.getResources().getColor(com.mylib.libcore.R.color.text_gray))
                .setTextColor(context.getResources().getColor(com.mylib.libcore.R.color.text_gray))
                .setSelectedTextColor(context.getResources().getColor(com.mylib.libcore.R.color.text_black))
                .setDividerColor(context.getResources().getColor(com.mylib.libcore.R.color.line_color))
                .setDividerWidth(2f)
                .setKeepLastSelected(true)
                .setShowYMDHMLabel(false)
                .setTextSize(context.getResources().getDimensionPixelSize(com.mylib.libcore.R.dimen.text_30px), context.getResources().getDimensionPixelSize(com.mylib.libcore.R.dimen.text_30px))
                .setShowType(DateTimePicker.ShowType.DAY)
                .build();
        dateTimePicker.setResultHandler(date -> getDataConstellation(date));
        dateTimePicker.show(new Date());
    }

    private void getDataConstellation(Date dateSelect){
        this.chooseData = dateSelect;
        if(chooseData != null){
            dataBinding.tvChooseData.setText(TimeUtils.getDateTime(chooseData)+"");
            String dataString = dataBinding.tvChooseData.getText().toString();
            if(dataString.length()>=10){
                mViewModel.getData(dataString.substring(0,4),dataString.substring(5,7),dataString.substring(8,10),xzNameToEnglish(constellationName));
            }
        }else {
            TimeUtils.stampToDate("请选择日期");
        }
    }

    //中文转英文
    private String xzNameToEnglish(String name){
        //  十二星座英文名：aries 白羊座, taurus 金牛座, gemini 双子座, cancer 钜蟹座, leo 狮子座, virgo 处女座, libra 天平座, scorpio 天蝎座, sagittarius 射手座, capricorn ,摩羯座, aquarius 水瓶座, pisces 双鱼座。
        String englishName = "";
        switch (name){
            case "白羊座":
                englishName = "aries";
                break;
            case "金牛座":
                englishName = "taurus";
                break;
            case "双子座":
                englishName = "gemini";
                break;
            case "钜蟹座":
                englishName = "cancer";
                break;
            case "狮子座":
                englishName = "leo";
                break;
            case "处女座":
                englishName = "virgo";
                break;
            case "天枰座":
                englishName = "libra";
                break;
            case "天蝎座":
                englishName = "scorpio";
                break;
            case "射手座":
                englishName = "sagittarius";
                break;
            case "摩羯座":
                englishName = "capricorn";
                break;
            case "水瓶座":
                englishName = "aquarius";
                break;
            case "双鱼座":
                englishName = "pisces";
                break;
        }
        return englishName;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void itemTextClink(int index, String text, String id, String type) {
        if(!UIUtils.isEmpty(text)){
            constellationName = text;
            dataBinding.tvChooseXz.setText(text+"");
            AppContext.getInstance().getAppPreferencesHelper().setKeyValue("constellation",text);
            getDataConstellation(chooseData);
        }
    }
}
